package org.example.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.domain.po.ErpMenu;
import org.example.domain.po.ErpRole;
import org.example.domain.po.ErpRoleMenu;
import org.example.domain.po.ErpUserRole;
import org.example.domain.vo.ErpMenuBtnVO;
import org.example.enums.ButtonType;
import org.example.service.ErpMenuService;
import org.example.service.ErpRoleMenuService;
import org.example.mapper.ErpRoleMenuMapper;
import org.example.service.ErpRoleService;
import org.example.service.ErpUserRoleService;
import org.example.utils.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author DELL
* @description 针对表【erp_role_menu(角色/模块关系表)】的数据库操作Service实现
* @createDate 2024-10-11 10:04:58
*/
@Service
@Transactional
public class ErpRoleMenuServiceImpl extends ServiceImpl<ErpRoleMenuMapper, ErpRoleMenu>
    implements ErpRoleMenuService{
    @Resource
    private ErpMenuService erpMenuService;
    
    @Resource
    private ErpUserRoleService userRoleService;
    
    @Autowired
    @Lazy
    private ErpRoleService roleService;
    
    @Override
    public List<ErpMenuBtnVO> rolePushBtnById(Long id) {
        List<ErpMenuBtnVO> erpMenuBtnVOS = baseMapper.rolePushBtnById(id);
        erpMenuBtnVOS.forEach(e -> e.setPushBtnMap(EnumUtil.getByValueMap(ButtonType.class, e.getMenuPushBtn())));
        return erpMenuBtnVOS;
    }
    
    @Override
    public List<ErpMenuBtnVO> getRoleMenuBtnStr(Long roleId) {
        List<ErpMenuBtnVO> erpMenuBtnVOS = baseMapper.getRoleMenuBtnStr(roleId);
        erpMenuBtnVOS.forEach(e -> e.setPushBtnMap(EnumUtil.getByValueMap(ButtonType.class, e.getMenuPushBtn())));
        return erpMenuBtnVOS;
    }
    
    @Override
    public int upRoleMenuStr(Map<String, Object> data) {
        // 角色菜单关系表的权限
        Map<String, String> mapObject = Convert.convert(Map.class, data.get("mapObject"));
        Long roleId = Convert.convert(Long.class, data.get("roleId"));
        mapObject.forEach((key, value) -> {
            LambdaQueryWrapper<ErpRoleMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ErpRoleMenu::getId, Long.parseLong(key));
            // 角色菜单类
            ErpRoleMenu erpRoleMenu = baseMapper.selectOne(wrapper);
            
            // 查询出roleMenu表parentId为erpRoleMenu.getMenu的menu集合
            List<ErpMenu> menus = erpMenuService.queryMenuByParentId(erpRoleMenu.getMenuId());
            
            List<Long> menuIds = menus.stream().map(ErpMenu::getId).collect(Collectors.toList());
            // 如果权限按钮包含1(编辑，增删改操作)
            if (value.contains("1")){
                // 查询该角色该页面的增删改授权
                List<ErpRoleMenu> list1 = lambdaQuery().in(ErpRoleMenu::getMenuId, menuIds).eq(ErpRoleMenu::getRoleId, roleId).list();
                if (menuIds.size() != list1.size()){
                    // 授权
                    List<ErpRoleMenu> roleMenus = new ArrayList<>();
                    menuIds.forEach(menuId -> roleMenus.add(new ErpRoleMenu(roleId, menuId)));
                    saveBatch(roleMenus);
                }
            } else {
                // 如果不包含，删除
                lambdaUpdate().eq(ErpRoleMenu::getRoleId, roleId).in(ErpRoleMenu::getMenuId, menuIds).remove();
            }
        });
        lambdaUpdate().set(ErpRoleMenu::getBtnStr, null)
                .eq(ErpRoleMenu::getRoleId, roleId).update();
        if (CollUtil.isEmpty(mapObject)) return 1;
        return baseMapper.upRoleMenuStr(mapObject);
    }
    
    @Override
    public List<String> getBtnStr(Long userId, Long menuId) {
        ErpRole role = roleService.getRoleByUserId(userId);
        // 租户
        if (role.getName().equals("租户")){
            return Arrays.asList("编辑", "审核", "反审核");
        }
        ErpRoleMenu one = lambdaQuery().eq(ErpRoleMenu::getMenuId, menuId).eq(ErpRoleMenu::getRoleId, role.getId()).one();
        Map<Integer, String> byValueMap = EnumUtil.getByValueMap(ButtonType.class, one.getBtnStr());
        
        List<String> list = new ArrayList<>();
        byValueMap.forEach((key, value) -> {
            if (key != 1){
                list.add(value);
            }
        });
        return list;
    }
}
